'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports ua
Imports System.IO
Imports System.Drawing

Module Module1
    Private imgWarning As New Bitmap(GetType(Module1).Assembly.GetManifestResourceStream("UAImage.Warning.png"))

    Private AppDataPath As String = GetAppDataPath()

    'ISPReturnData
    Private ISPReturnData As ISPReturnData

    'configuration data
    Private NewConfigurationData As New ConfigurationData

    'nic data
    Private NicValueDown As New System.Diagnostics.PerformanceCounter
    Private NicValueUp As New System.Diagnostics.PerformanceCounter
    Private NicUsageDown As Double
    Private NicUsageUp As Double

    Sub Main()
        Try
            'already running, exit
            Dim myProcesses As Process() = Process.GetProcessesByName("UAImage")
            If myProcesses.Length > 1 Then End
            myProcesses = Nothing

            'check that the necessary files exist
            If Dir(AppDataPath & "usageagent.cfg") = "" Then
                MessageBox.Show("Cannot find usageagent.cfg. Usage Agent Configuration must be run prior to using this application.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End
            End If

            '############################################################

            'load config
            If LoadConfiguration(NewConfigurationData) = True Then
                'if isp has not been validated, abort
                If NewConfigurationData.ISPValidated = False Then
                    MessageBox.Show("The connection to your ISP has not been validated. You must validate prior to running Usage Agent. Please re-run UA Configuration.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    End
                End If

                'isp usage
                If NewConfigurationData.RetrieveNICData = True Then
                    'enable tcp performance if currently disabled
                    RegEnablePerfCounter()

                    Try
                        NicValueDown.CategoryName = "Network Interface"
                        NicValueDown.CounterName = "Bytes Received/sec"
                        NicValueDown.MachineName = "."

                        NicValueUp.CategoryName = "Network Interface"
                        NicValueUp.CounterName = "Bytes Sent/sec"
                        NicValueUp.MachineName = "."

                        'set nic
                        NicValueDown.InstanceName = NewConfigurationData.NICName
                        NicValueUp.InstanceName = NewConfigurationData.NICName
                    Catch ex As Exception
                        NewConfigurationData.RetrieveNICData = False
                    End Try
                End If

                'get ISP data
                ISPReturnData = New ISPData(NewConfigurationData).ISPReturnData

                'check for error
                If ISPReturnData.Status = 3 And My.Computer.FileSystem.FileExists(AppDataPath & "uaimage.png") Then
                    'get last image
                    Dim myStream As Stream = File.Open(AppDataPath & "uaimage.png", FileMode.Open, FileAccess.Read, FileShare.Read)
                    Dim SourceImage As Image = Image.FromStream(myStream)
                    myStream.Close()

                    'embed warning icon
                    Dim g As Graphics = Graphics.FromImage(SourceImage)
                    g.DrawImageUnscaled(imgWarning, 1, 0)
                    g.Dispose()

                    'save back to file
                    SourceImage.Save(AppDataPath & "uaimage.png")
                    SourceImage.Dispose()
                Else
                    'check for text log output
                    If NewConfigurationData.ExportTextLog = True Then ExportTextLog(ISPReturnData, NewConfigurationData)

                    If NewConfigurationData.RetrieveNICData = True Then
                        'get NIC data
                        NicUsageDown = CDbl(NicValueDown.RawValue.ToString)
                        NicUsageUp = CDbl(NicValueUp.RawValue.ToString)
                    End If

                    'if no errors, save data
                    If ISPReturnData.Status = 2 Then
                        WriteLog(ISPReturnData, NewConfigurationData.RetrieveNICData, NicUsageDown, NicUsageUp)
                    End If

                    'create image
                    CreateImageFile(NewConfigurationData, ISPReturnData)
                End If

                'save config
                SaveConfiguration(NewConfigurationData)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

End Module
